#!/usr/bin/env bash

if [ "$#" -ne 2 ]; then
    echo "Usage: ./reencode.sh [input dir] [output dir]"
fi

indir=$1
outdir=$2

mkdir outdir
if [[ ! -d "${outdir}" ]]; then
    echo "${outdir} doesn't exist. Creating it.";
    mkdir -p ${outdir}
fi

for c in $(ls ${indir})
do
    for inname in $(ls ${indir}/${c}/*mp4)
    do
        class_path="$(dirname "$inname")"
        class_name="${class_path##*/}"

        outname="${outdir}/${class_name}/${inname##*/}"
        outname="${outname%.*}.mp4"

        mkdir -p "$(dirname "$outname")"
        # MPEG是编码标准, 而mp4, avi等是支持MPEG标准的文件格式
        # -vf scale=340:256,setsar=1:1  参见man ffmpeg-all, scaling默认使用bicubic算法, Sample Aspect Ratio 像素长宽比
        # -q:v 1   vedio encoding quality, [-1.0, 10.0], 此处设置的quality较低
        # -f rawvideo 设置 muxer 不进行混合
        ffmpeg -i ${inname} -vf scale=340:256,setsar=1:1 -q:v 1 -c:v mpeg4 -f rawvideo ${outname}
    done
done
